<?php

namespace app\client\controller\learn;

use app\common\controller\Frontend;
use app\common\enums\AccountTypeEnum;
use app\common\enums\LearnMeansEnum;
use app\common\model\banxi\learn\LearnSayQuestionAnswerRecord;
use app\common\model\banxi\learn\LearnSkRecord as LearnSkRecordModel;
use app\common\service\learn\LearnSkRecordService;

/*
 * 上课记录
 * */

class LearnSkRecord extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    //我的上课记录
    public function listXkPage()
    {
        $limit = $this->request->get('limit', 20);
        $index = $this->request->get('index', 1);
        $searchText = $this->request->get('searchText', '');

        $studentId = $this->auth->id;
        $LearnSkRecordModel = new LearnSkRecordModel();
        $list = $LearnSkRecordModel->getListXkPage($index, $limit, $studentId, AccountTypeEnum::STUDENT, $searchText);

        $this->ok('操作成功', result_row($list));
    }

    //学习报告(全部)
    public function getSkRecordReport()
    {
        $skRecordId = $this->request->get('skRecordId');
        if (!$skRecordId) {
            $this->fail('请选择上课记录');
        }
        $LearnSkRecordService = new LearnSkRecordService();
        $result = $LearnSkRecordService->questionReportAll($skRecordId);

        $this->ok('ok', $result);
    }

    //课程学习
    public function skRecordWordMemorization()
    {
        $params = $this->request->param(['index', 'limit', 'skRecordId']);
        if (!$params['skRecordId']) {
            $this->fail('请选择上课记录');
        }
        $studentId = $this->auth->id;

        $skRecordId = $params['skRecordId'];
        $pageParams = get_page_params($params);
        $learnSayQuestionAnswerRecordModel = new LearnSayQuestionAnswerRecord();

        $list = $learnSayQuestionAnswerRecordModel->alias('a')
            ->join('banxi_learn_say_question_record b', 'a.recordId = b.id')
            ->join('banxi_say_teach_sentence c', 'a.sayTeachSentenceId = c.id')
            ->where([
                ['b.skRecordId', '=', $skRecordId],
                ['b.isSubmit', '=', 1],
                ['b.studentId', '=', $studentId],
                ['b.learnMeans', '=', LearnMeansEnum::E1],
                ['b.del', '=', 0],
                ['a.isCorrect', '>', -1],
                ['a.del', '=', 0],
                ['c.coreVocabulary', '<>', ''],
                ['c.del', '=', 0],
            ])
            ->whereNotNull('c.coreVocabulary')
            ->field('a.sayTeachSentenceId, a.sentenceEn, a.isCorrect, c.coreVocabulary')
            ->order(['a.id' => 'desc'])
            ->paginate([
                'list_rows' => $pageParams['limit'],
                'page' => $pageParams['page']
            ]);
        $learnSkRecordModel = new LearnSkRecordModel();
        $list = $learnSkRecordModel->getCoreVocabularySplit($list);
        $this->ok('ok', $list);

    }

    public function wordMemorization()
    {
        $params = $this->request->param(['index', 'limit']);
        $studentId = $this->auth->id;
        $pageParams = get_page_params($params);
        $learnSayQuestionAnswerRecordModel = new LearnSayQuestionAnswerRecord();
        $list = $learnSayQuestionAnswerRecordModel->alias('a')
            ->join('banxi_learn_say_question_record b', 'a.recordId = b.id')
            ->join('banxi_say_teach_sentence c', 'a.sayTeachSentenceId = c.id')
            ->where([
                ['b.isSubmit', '=', 1],
                ['b.studentId', '=', $studentId],
                ['b.del', '=', 0],
                ['b.learnMeans', '=', LearnMeansEnum::E1],
                ['a.isCorrect', '>', -1],
                ['a.del', '=', 0],
                ['c.coreVocabulary', '<>', ''],
                ['c.del', '=', 0],
            ])
            ->whereNotNull('c.coreVocabulary')
            ->field('a.sayTeachSentenceId, a.sentenceEn, a.isCorrect, c.coreVocabulary')
            ->order(['a.id' => 'desc'])
            //     ->fetchSql()
            //     ->select();
            // dump($list);
            // die;
            ->paginate([
                'list_rows' => $pageParams['limit'],
                'page' => $pageParams['page']
            ]);

        $learnSkRecordModel = new LearnSkRecordModel();
        $list = $learnSkRecordModel->getCoreVocabularySplit($list);
        $this->ok('ok', $list);
    }
}